package common.selfreemark;

import freemarker.cache.FileTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import org.hamcrest.SelfDescribing;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.enterprise.inject.New;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class MyFremarkTest {

    private  static  Logger logger = LoggerFactory.getLogger(MyFremarkTest.class);

    private static final String TEMPLATE_PATH = "src/main/java/common/selfreemark/ftl";
    private static final String CLASS_PATH = "src/main/java/common/selfreemark";

    public static void main(String[] args) throws IOException, TemplateException {
        Configuration.getVersionNumber();
        logger.info("freeMarkVersion====> " + Configuration.getVersionNumber());
        Writer writer = null;
        //获取configuration
        Configuration configuration = SelFreeMarkerConfiguration.getInstance();

        configuration.setDirectoryForTemplateLoading(new File(TEMPLATE_PATH));
//        Map<String,Object> dataMap = new HashMap<>();
//        dataMap.put("classPath","common.selfreemark");
//        dataMap.put("className","HelloDemo");
//        dataMap.put("helloWorld", "通过简单的 <代码自动生产程序> 演示 FreeMarker的HelloWorld！");
//
//        Template template = configuration.getTemplate("hello.ftl");
//        // step5 生成数据
//        File docFile = new File(CLASS_PATH + "\\" + "HelloDemo.java");
////        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
//        // step6 输出文件
//        writer = new StringWriter();
//
//        template.process(dataMap, writer);

//        writer.toString();
//        System.out.println("qwasd===> \n" +writer.toString());
//        System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^AutoCodeDemo.java 文件创建成功 !");
        System.out.println("短信内容测试");

        Map<String,Object> SMSData = new HashMap<String,Object>();
        SMSData.put("branchName","甘肃股份有限公司");
        SMSData.put("branchNo","00999");
        SMSData.put("money","1000000");
        SMSData.put("scale","0.212");

        Template template = configuration.getTemplate("blueSMS.ftl");
        writer = new StringWriter();

        template.process(SMSData,writer);

        System.out.println("短信内容 \n" + writer.toString());

    }
}
